package edu.bmstu.rslab;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Point2D;
import java.util.ArrayList;

/**
 * Created by Chikh (using IntelliJ IDEA).
 * Date: 14.04.2009
 * Time: 18:20:25
 */
public class UIFrame extends JFrame {

    public UIFrame() {

        final int POSITIONING_DELAY = 300;
        final int MOVING_DELAY = 20; //for stub implementation

        final MobileRobot robot = new MobileRobotStubImpl();
        final Camera camera = new CameraStubImpl(robot); //Setting up the camera implementation
        final Guidance guidance = new Guidance(robot);
        final ArrayList<Point2D> checkPoints = new ArrayList<Point2D>();

        setTitle("Positioning system model");
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize().getSize();
        setSize(screenSize.width, screenSize.height/2);
        setLocation(0, screenSize.height/4);

        VideoPanel videoPanel = new VideoPanel(camera);
        CoordinateSystemPanel coordinateSystemPanel = new CoordinateSystemPanel();
        JPanel outputPanel = new JPanel(new GridLayout(1, 2));

        outputPanel.add(videoPanel);
        outputPanel.add(coordinateSystemPanel);
        add(outputPanel, BorderLayout.CENTER);

        Translator translator = new Translator();
        Detector detector = new Detector();
        Detector directionDetector = new Detector();
        Action positioningAction = new positioningAction(camera, coordinateSystemPanel, videoPanel, guidance,
                detector, directionDetector, translator);
        Action movingAction = new movingAction(robot, videoPanel);

        Timer positioningTimer = new Timer(POSITIONING_DELAY, positioningAction);
        Timer movingTimer = new Timer(MOVING_DELAY, movingAction);

        Action startAction = new startAction(positioningAction, positioningTimer, movingTimer);
        Action stopAction = new stopAction(positioningTimer, movingTimer, startAction);
        Action setPathAction = new setPathAction(videoPanel, camera, guidance, checkPoints, coordinateSystemPanel,
                startAction, stopAction, translator, directionDetector);
        Action selectDirectionMaskAction = new selectDirectionMaskAction(videoPanel, camera, setPathAction,
                directionDetector);
        Action selectPositionMaskAction = new selectPositionMaskAction(detector, translator, videoPanel, camera,
                checkPoints, coordinateSystemPanel, selectDirectionMaskAction);
        Action setRobotPositionAction = new SetRobotPositionAction(coordinateSystemPanel, robot,
                selectPositionMaskAction, videoPanel);
        BasisOfLCS lcsBasis = null;
        Action setCSAction = new SetCSAction(videoPanel, setRobotPositionAction, camera, lcsBasis, translator);
        Action exitAction = new AbstractAction("Exit") {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        };

        JPanel buttonPanel = new JPanel();
        JButton setCSButton = new JButton(setCSAction);
        JButton setRobotPositionButton = new JButton(setRobotPositionAction);
        JButton selectPositionMaskButton = new JButton(selectPositionMaskAction);
        JButton selectDirectionMaskButton = new JButton(selectDirectionMaskAction);
        JButton setPathButton = new JButton(setPathAction);
        JButton startButton = new JButton(startAction);
        JButton stopButton = new JButton(stopAction);
        JButton exitButton = new JButton(exitAction);

        buttonPanel.add(setCSButton);
        buttonPanel.add(setRobotPositionButton);
        buttonPanel.add(selectPositionMaskButton);
        buttonPanel.add(selectDirectionMaskButton);
        buttonPanel.add(setPathButton);
        buttonPanel.add(startButton);
        buttonPanel.add(stopButton);
        buttonPanel.add(exitButton);
        add(buttonPanel, BorderLayout.NORTH);
    }
}